<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>吉他管理</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f4f4f4;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 20px;
            border-bottom: 1px solid #eee;
        }
        .nav-links {
            display: flex;
            gap: 20px;
        }
        .nav-links a {
            text-decoration: none;
            color: #333;
            padding: 8px 16px;
            border-radius: 4px;
            transition: background-color 0.3s;
        }
        .nav-links a:hover {
            background-color: #f0f0f0;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        th {
            background-color: #4A90E2;
            color: white;
        }
        tr:hover {
            background-color: #f5f5f5;
        }
        .btn {
            padding: 8px 16px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            color: white;
            transition: background-color 0.3s;
        }
        .btn-add {
            background-color: #4CAF50;
        }
        .btn-edit {
            background-color: #2196F3;
        }
        .btn-delete {
            background-color: #f44336;
        }
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
        }
        .modal-content {
            background-color: white;
            margin: 15% auto;
            padding: 20px;
            width: 50%;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
        }
        .form-group input {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .btn-cart {
            background-color: #FF9800;
        }

        .cart-notification {
            position: fixed;
            top: 20px;
            right: 20px;
            padding: 15px;
            background-color: #4CAF50;
            color: white;
            border-radius: 4px;
            display: none;
            animation: slideIn 0.3s ease-out;
        }

        @keyframes slideIn {
            from {
                transform: translateX(100%);
            }
            to {
                transform: translateX(0);
            }
        }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1>吉他管理</h1>
        <div class="nav-links">
            <a href="customer?action=list">顾客管理</a>
            <a href="guitar?action=list">吉他管理</a>
            <a href="purchase?action=list">购买记录</a>
        </div>
        <button class="btn btn-add" onclick="showAddModal()">添加吉他</button>
    </div>

    <table>
        <thead>
        <tr>
            <th>ID</th>
            <th>品牌</th>
            <th>类型</th>
            <th>制造商</th>
            <th>价格</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <c:forEach items="${guitars}" var="guitar">
            <tr>
                <td>${guitar.id}</td>
                <td>${guitar.brand}</td>
                <td>${guitar.type}</td>
                <td>${guitar.manufacturer}</td>
                <td>￥${guitar.price}</td>
                <td>
                    <button class="btn btn-edit" onclick="showEditModal(${guitar.id})">编辑</button>
                    <button class="btn btn-delete" onclick="deleteGuitar(${guitar.id})">删除</button>
                    <button class="btn btn-cart" onclick="addToCart(${guitar.id})">加入购物车</button>
                </td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</div>

<!-- 添加/编辑吉他的模态框 -->
<div id="guitarModal" class="modal">
    <div class="modal-content">
        <h2 id="modalTitle">添加吉他</h2>
        <form id="guitarForm" action="guitar" method="post">
            <input type="hidden" name="action" id="formAction" value="add">
            <input type="hidden" name="id" id="guitarId">
            <div class="form-group">
                <label>品牌：</label>
                <input type="text" name="brand" id="brand" required>
            </div>
            <div class="form-group">
                <label>类型：</label>
                <input type="text" name="type" id="type" required>
            </div>
            <div class="form-group">
                <label>制造商：</label>
                <input type="text" name="manufacturer" id="manufacturer" required>
            </div>
            <div class="form-group">
                <label>价格：</label>
                <input type="number" name="price" id="price" step="0.01" required>
            </div>
            <button type="submit" class="btn btn-add">保存</button>
            <button type="button" class="btn btn-delete" onclick="hideModal()">取消</button>
        </form>
    </div>
</div>

<div id="cartNotification" class="cart-notification">
    已添加到购物车
</div>

<script>
    function showAddModal() {
        document.getElementById('modalTitle').textContent = '添加吉他';
        document.getElementById('formAction').value = 'add';
        document.getElementById('guitarForm').reset();
        document.getElementById('guitarModal').style.display = 'block';
    }

    function showEditModal(id) {
        fetch('guitar?action=get&id=' + id)
            .then(response => response.json())
            .then(guitar => {
                document.getElementById('modalTitle').textContent = '编辑吉他';
                document.getElementById('formAction').value = 'edit';
                document.getElementById('guitarId').value = guitar.id;
                document.getElementById('brand').value = guitar.brand;
                document.getElementById('type').value = guitar.type;
                document.getElementById('manufacturer').value = guitar.manufacturer;
                document.getElementById('price').value = guitar.price;
                document.getElementById('guitarModal').style.display = 'block';
            });
    }

    function hideModal() {
        document.getElementById('guitarModal').style.display = 'none';
    }

    function deleteGuitar(id) {
        if (confirm('确定要删除这把吉他吗？')) {
            window.location.href = 'guitar?action=delete&id=' + id;
        }
    }

    function addToCart(guitarId) {
        fetch('cart?action=add&guitarId=' + guitarId)
            .then(response => response.json())
            .then(data => {
                if(data.success) {
                    showNotification('已添加到购物车');
                } else {
                    showNotification(data.message || '添加失败');
                }
            });
    }

    function showNotification(message) {
        const notification = document.getElementById('cartNotification');
        notification.textContent = message;
        notification.style.display = 'block';
        setTimeout(() => {
            notification.style.display = 'none';
        }, 2000);
    }
</script>
</body>
</html>